from PyQt4 import QtCore, QtGui

from limber.ui.dialogs import utils


class ErrorDialog(QtGui.QDialog):
    def __init__(self, parent, title, text):
        super(ErrorDialog, self).__init__(parent)

        self.label = QtGui.QLabel(text)
        close_button = QtGui.QPushButton('Close')

        button_layout = QtGui.QHBoxLayout()
        button_layout.addStretch()
        button_layout.addWidget(close_button)
        button_layout.addStretch()

        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.label)
        layout.addLayout(button_layout)
        self.setLayout(layout)

        close_button.click.connect(self.close)


class QuerySaveFileNameDialog(QtGui.QFileDialog):

    def __init__(self, parent=None, dir='.'):
        super(QuerySaveFileNameDialog, self).__init__(parent, 'Save File',
                                                dir)

    def accept(self):
        '''Accepts the dialog.'''

        selected_files = [unicode(fname) for fname in self.selectedFiles()]
        if not selected_files:
            self.filename = False
        elif len(selected_files) > 1:
            dlg = ErrorDialog(self, 'Error: Multiple files selected!',
                              'Selection of multiple files is not supported!')
            dlg.exec_()
            return
        else:
            self.filename = selected_files[0]

        super(QuerySaveFileNameDialog, self).accept()


utils.register_dialog(QuerySaveFileNameDialog,'query-save-filename')